package com.longrise.bymodule.page.importcontacts;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.bymodule.R;
import com.longrise.common.utils.PrintLog;

import java.util.ArrayList;

/**
 * Created by callxkj on 2017/7/7.
 * 联系人的rcvAdapter
 */

public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ContactsViewHolder> {

    private static final String TAG = "ContactsAdapter";
    private final Context mContext;
    private ArrayList<ContactsBean> mDataList = new ArrayList<>();
    private ItemCheckImageClickListener mListener;

    public ContactsAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(ArrayList<ContactsBean> list) {
        mDataList.clear();
        mDataList.addAll(list);
        notifyDataSetChanged();
    }


    @Override
    public ContactsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.by_module_item_contacts, null);

        return new ContactsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ContactsViewHolder holder, final int position) {
        try {
            ContactsBean contactsBean = mDataList.get(position);
            if (position == 0 || !mDataList.get(position - 1).letter.equals(contactsBean.letter)) {
                holder.rl1.setVisibility(View.VISIBLE);
                holder.tv1.setText(contactsBean.letter);
            } else {
                holder.rl1.setVisibility(View.GONE);
            }
            if (position == (mDataList.size() - 1) || !mDataList.get(position + 1).letter.equals(contactsBean.letter)) {
                holder.v_div2.setVisibility(View.GONE);
            } else {
                holder.v_div2.setVisibility(View.VISIBLE);
            }
            GradientDrawable gradientDrawable = (GradientDrawable) holder.tv_simple.getBackground();
            gradientDrawable.setColor(Color.parseColor(contactsBean.backgorund));
            holder.tv_simple.setText(contactsBean.firstword);
            holder.tv_name.setText(contactsBean.name);
            if ("1".equals(contactsBean.isadd)) {
                holder.tv_ytj.setVisibility(View.VISIBLE);
                holder.iv_check.setVisibility(View.GONE);
            } else {
                if (contactsBean.isChecked) {
                    holder.iv_check.setImageResource(R.drawable.by_module_check_on);
                } else {
                    holder.iv_check.setImageResource(R.drawable.by_module_check_none);
                }
                holder.tv_ytj.setVisibility(View.GONE);
                holder.iv_check.setVisibility(View.VISIBLE);
                holder.iv_check.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null) {
                             mListener.onClick(position);
                        }
                    }
                });
            }
        } catch (Exception e) {
            PrintLog.d(TAG, e.toString());
        }
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    class ContactsViewHolder extends RecyclerView.ViewHolder {

        private final RelativeLayout rl1;
        private final TextView tv1;
        private final TextView tv_simple;
        private final TextView tv_name;
        private final TextView tv_ytj;
        private final ImageView iv_check;
        private final View v_div2;

        public ContactsViewHolder(View itemView) {
            super(itemView);
            rl1 = (RelativeLayout) itemView.findViewById(R.id.item_contacts_div1);
            tv1 = (TextView) itemView.findViewById(R.id.item_contacts_tv1);
            tv_simple = (TextView) itemView.findViewById(R.id.item_contacts_simple_tv);
            tv_name = (TextView) itemView.findViewById(R.id.item_contacts_name_tv);
            tv_ytj = (TextView) itemView.findViewById(R.id.item_contacts_ytj_tv);
            iv_check = (ImageView) itemView.findViewById(R.id.item_contacts_check_iv);
            v_div2 = itemView.findViewById(R.id.item_contacts_div2);
        }
    }

    public void setOnItemCheckImageClickListener(ItemCheckImageClickListener listener) {
        this.mListener = listener;
    }

    interface ItemCheckImageClickListener {
        void onClick(int position);
    }
}
